/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 29, 2011 12:45:53 PM
 */
package carrello.model.association;

import carrello.model.entities.*;
import carrello.util.EntityManagerUtil;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.*;
@NamedQueries({
	@NamedQuery(name = "getAllAssociationsBySourceId", query = "from CategoryAssociation where sourceid = :sid"),
      	@NamedQuery(name = "getAllAssociationsByDestinationId", query = "from CategoryAssociation where destinationid = :des_id"),
                  @NamedQuery(name = "getAssociatedCategories", query = "from CategoryAssociation where source_id = :s OR destination_id = :d")
})

@Entity
@Table(name = "CategoryAssociation")
public class CategoryAssociation {
    @Id
    @Column(name = "association_id")
    private int associationId;
    @Column(name = "source_id")
    private int sourceid;
    @Column(name = "destination_id")
    private int destinationid;
    private String association_name;

    public CategoryAssociation() {
    }

    public CategoryAssociation(int sourceid, int destinationid, String association_name) {
        this.sourceid = sourceid;
        this.destinationid = destinationid;
        this.association_name = association_name;
    }

    /**
     * @return the associationId
     */
    public int getAssociationId() {
        return associationId;
    }

    /**
     * @param associationId the associationId to set
     */
    public void setAssociationId(int associationId) {
        this.associationId = associationId;
    }

    /**
     * @return the sourceid
     */
    public int getSourceid() {
        return sourceid;
    }

    /**
     * @param sourceid the sourceid to set
     */
    public void setSourceid(int sourceid) {
        this.sourceid = sourceid;
    }

    /**
     * @return the destinationid
     */
    public int getDestinationid() {
        return destinationid;
    }

    /**
     * @param destinationid the destinationid to set
     */
    public void setDestinationid(int destinationid) {
        this.destinationid = destinationid;
    }

    /**
     * @return the association_name
     */
    public String getAssociation_name() {
        return association_name;
    }

    /**
     * @param association_name the association_name to set
     */
    public void setAssociation_name(String association_name) {
        this.association_name = association_name;
    }
    public static LinkedList<Integer> getAssociatedCategories ( int id ) {
        List<CategoryAssociation> assoc = EntityManagerUtil.getEntityManager().getQuery("getAssociatedCategories")
                .setParameter("s", id)
                .setParameter("d", id).list();
        LinkedList<Integer> result = new LinkedList<Integer> ();
        for ( CategoryAssociation a: assoc ) {
            if ( a.getSourceid() != id && !result.contains (a.getSourceid()))
                result.add (a.getSourceid());
            else if ( a.getDestinationid() != id && !result.contains (a.getDestinationid()))
                result.add (a.getDestinationid());
        }
        return result;
    }
    public static CategoryAssociation getCategoryAssociationsBySourceId(int sourceid) {
         return (CategoryAssociation)EntityManagerUtil.getEntityManager().getQuery("getAllAssociationsBySourceId")
                                    .setParameter("sid", sourceid)
                                    .uniqueResult();
    } // getCategoryAssociationsBySourceId
    
    public static CategoryAssociation getCategoryAssociationsByDestinationId(int destinationid) {
         return (CategoryAssociation)EntityManagerUtil.getEntityManager().getQuery("getAllAssociationsByDestinationId")
                                    .setParameter("des_id", destinationid)
                                    .uniqueResult();
    } // getCategoryAssociationsByDestinationId
    
    public static List<CategoryAssociation> getAssociatedCategory ( int id ) {

         return EntityManagerUtil.getEntityManager().getQuery("getAssociatedCategories")
                .setParameter("s", id)
                .setParameter("d", id).list();
    }
}
